package com.app.micaihu.view.user.userinfo.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseListActivity;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.infor.AttentionAndFansBean;
import com.app.micaihu.bean.infor.AttentionStautsBean;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.utils.DialogUtils;
import com.app.micaihu.view.user.userinfo.adapter.AttentionAndFansAdapter;
import com.app.utils.util.ToastUtils;
import com.blankj.utilcode.util.ActivityUtils;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Guo on 2016/8/26.
 * <p>
 * 关注和粉丝的列表activity
 */
public class AttentionAndFansListActivity extends BaseListActivity<AttentionAndFansBean> {

    private String attentionOrFans;//当前的状态吗
    private String toUid = "";//toUid
    private boolean isAttion;//是否是关注


    @Override
    public void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (intent != null) {
            isAttion = intent.getBooleanExtra(Config.Extra.PARAMETER1, true);
            toUid = intent.getStringExtra(Config.Extra.PARAMETER2);
        }

        super.onCreate(savedInstanceState);
        //UserInforManager.getInstance().addObserver(this);
    }


    @Override
    protected void changeView() {
        super.changeView();
        //1是关注;2是粉丝
        if (isAttion) {
            setTitle(getResources().getString(R.string.homepage_attention));
            attentionOrFans = "1";
        } else {
            setTitle(getResources().getString(R.string.homepage_fans));
            attentionOrFans = "2";
        }

    }

    @Override
    protected void changeData(final boolean isRefresh) {
        //if(!UserInforManager.getInstance().isLogin()) return;

        Map<String, String> params = new HashMap<>();
        if (UserInforManager.getInstance().isLogin()) {
            params.put("uid", UserInforManager.getInstance().getUserInfor().getUid());
        } else {
            params.put("uid", "0");
        }

        params.put("toUid", toUid);
        params.put("type", attentionOrFans);
        params.put("page", dataPage + "");

        HttpListener<DataBean<List<AttentionAndFansBean>>> listListener = new HttpListener<DataBean<List<AttentionAndFansBean>>>() {
            @Override
            public void onStart() {
                super.onStart();
                setLoadView(LOADING, null);
            }

            @Override
            public void onError(VolleyError error) {
                setLoadView(NO_NET, AppApplication.getInstance().getString(R.string.neterror_click));
            }

            @Override
            public void onSuccess(DataBean<List<AttentionAndFansBean>> response) {
                if (response.noError() && response.getData() != null) {
                    List<AttentionAndFansBean> attentionAndFansList = response.getData();
                    if (attentionAndFansList.size() > 0) {
                        if (dataList == null) {
                            dataList = new ArrayList<AttentionAndFansBean>();
                        }
                        if (isRefresh) {
                            //清空数据
                            dataList.clear();
                        }
                        dataList.addAll(attentionAndFansList);

                        if (mAdapter == null) {
                            mAdapter = new AttentionAndFansAdapter(dataList, mActivity, "1", isOther());
                            mListView.setAdapter(mAdapter);
                        } else {
                            mAdapter.notifyDataSetChanged();
                        }
                        setLoadView(LOADSUCES, null);
                    } else {
                        if (isRefresh && dataList != null) {
                            dataList.clear();
                        }
                        if (dataList == null || dataList.size() == 0) {
                            if (mAdapter != null) mAdapter.notifyDataSetChanged();

                            if ("1".equals(attentionOrFans)) {
                                if (isOther()) {
                                    setLoadView(DATAEMPTY, TypeConstant.EMPTYVIEW_DEFAULT, getResources().getString(R.string.homepage_other_noattention));
                                } else {
                                    setLoadView(DATAEMPTY, TypeConstant.EMPTYVIEW_DEFAULT, getResources().getString(R.string.homepage_noattention));
                                }

                            } else if ("2".equals(attentionOrFans)) {
                                if (isOther()) {
                                    setLoadView(DATAEMPTY, TypeConstant.EMPTYVIEW_DEFAULT, getResources().getString(R.string.homepage_other_otnofans));
                                } else {
                                    setLoadView(DATAEMPTY, TypeConstant.EMPTYVIEW_DEFAULT, getResources().getString(R.string.homepage_nofans));
                                }

                            }
                        } else {
                            setLoadView(LOADSUCES, getString(R.string.homepage_msg_nomoremsg));
                        }
                    }

                } else {
                    setLoadView(DATAERROR, response.getNnderstoodMsg());
                }
            }
        };

        addPostCacheRequest(URIContant.ATTENTION_FANS, new TypeToken<DataBean<List<AttentionAndFansBean>>>() {
        }.getType(), params, listListener);


    }


    /**
     * 判定当前是谁的界面
     *
     * @return
     */
    private boolean isOther() {
        boolean isOther;
        if (!UserInforManager.getInstance().isLogin()) {
            isOther = true;
        } else {
            if (TextUtils.equals(toUid, UserInforManager.getInstance().getUserInfor().getUid())) {
                isOther = false;
            } else {
                isOther = true;
            }
        }
        return isOther;
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //因为有头部
        if (mListView.getRefreshableView().getHeaderViewsCount() > 0) {
            position = position - mListView.getRefreshableView().getHeaderViewsCount();
        }
        if (position >= dataList.size()) {
            position = dataList.size() - 1;
        } else if (position < 0) {
            position = 0;
        }
        AttentionAndFansBean attentionAndFansBean = dataList.get(position);
        if (attentionAndFansBean == null) return;
        String toUid = attentionAndFansBean.getUid();
        MyHomepageDynamicActivity.goToUserHomePage(ActivityUtils.getTopActivity(), toUid);
    }


    /**
     * @param actvity
     * @param attention_or_fans true是关注;false是粉丝
     * @param toUid
     */
    public static void goToAttentionAndFansActivity(Activity actvity, boolean attention_or_fans, String toUid, int code) {
        if (actvity == null || actvity.isFinishing()) {
            return;
        }
        Intent intent = new Intent();
        intent.setClass(actvity, AttentionAndFansListActivity.class);
        intent.putExtra(Config.Extra.PARAMETER1, attention_or_fans);
        intent.putExtra(Config.Extra.PARAMETER2, toUid);
        actvity.startActivityForResult(intent, code);

    }


    /**
     * 关注/取消关注
     *
     * @param view
     */
    private void attentionOrCancle(View view) {
        Object tag = view.getTag();
        if (tag instanceof AttentionAndFansBean) {
            final AttentionAndFansBean attentionAndFansBean = (AttentionAndFansBean) tag;
            Map<String, String> params = new HashMap<>();
            if (UserInforManager.getInstance().isLogin()) {
                params.put("uid", UserInforManager.getInstance().getUserInfor().getUid());
            } else {
                params.put("uid", "0");
            }

            params.put("toUid", attentionAndFansBean.getUid());
            String currentRequestType = "";
            if (TextUtils.equals(attentionAndFansBean.getStatus(), "0")) {     //未关注->点击去关注
                currentRequestType = "1";
            } else {//取消关注
                currentRequestType = "2";
            }
            params.put("type", currentRequestType);

            HttpListener<DataBean<AttentionStautsBean>> attentionListener = new HttpListener<DataBean<AttentionStautsBean>>() {
                @Override
                public void onStart() {
                    super.onStart();
                    DialogUtils.getInstance().showProgressDialog(AttentionAndFansListActivity.this, getResources().getString(R.string.homepage_attention_start));

                }

                @Override
                public void onError(VolleyError error) {
                    DialogUtils.getInstance().dismissProgressDialog();
                    ToastUtils.makeText(getResources().getString(R.string.neterror));
                }

                @Override
                public void onSuccess(DataBean<AttentionStautsBean> response) {
                    DialogUtils.getInstance().dismissProgressDialog();
                    if (response.noError() && response.getData() != null) {
                        AttentionStautsBean attentionStautsBean = response.getData();
                        if (attentionStautsBean != null && mAdapter != null) {
                            if (TextUtils.equals(attentionAndFansBean.getStatus(), "0")) {     //未关注->点击去关注
                                ToastUtils.makeText(getResources().getString(R.string.homepage_attentionsucc));
                            } else {//取消关注
                                ToastUtils.makeText(getResources().getString(R.string.homepage_attentioncanclesucc));
                            }

                            attentionAndFansBean.setStatus(attentionStautsBean.getStatus());
                            mAdapter.notifyDataSetChanged();

                        } else {
                            ToastUtils.makeText(getResources().getString(R.string.dataerror));
                        }
                    } else {
                        ToastUtils.makeText(getResources().getString(R.string.dataerror));

                    }
                }
            };

            addPostRequest(URIContant.ATTENTION_OR_NO, new TypeToken<DataBean<AttentionStautsBean>>() {
            }.getType(), params, attentionListener);
        }
    }


    /**
     * 防止看别人主页，并且位于未登录状态，点击关注-跳转登录回来刷新对于我的状态
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == UserInforManager.TO_LOGIN_REQUESTCODE && resultCode == RESULT_OK) {
            changeData(true);

        }
    }

}
